<template>
  <div class="record mx-5 my-5">
    <el-table
      :data="myOpenBoxRecord?.list"
      :empty-text="loading.list ? '数据加载中' : '暂无数据'"
      cell-class-name="custom-row"
      header-row-class-name="custom-row"
      header-cell-class-name="custom-header"
    >
      <el-table-column label="图片">
        <template #default="{ row }">
          <img :src="row.image_url" style="height: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="花费" prop="cost" />
      <el-table-column label="饰品名称" prop="item_name" />

      <el-table-column label="饰品价格" prop="price" />
      <el-table-column label="时间">
        <template #default="scope">
          {{ $dayjs.unix(scope.row.created_at).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
    </el-table>

    <div class="d-flex my-5 justify-center">
      <el-pagination
        :page-size="10"
        :total="total"
        hide-on-single-page
        background
        layout="prev, pager, next"
        @current-change="currentChange"
      />
    </div>
  </div>
</template>

<script>
import { UserService } from "../../../services/UserService";
import { onMounted, reactive, toRefs } from "vue";
import Empty from "@/components/empty/Index.vue";

export default {
  components: { Empty },
  setup() {
    const state = reactive({
      myOpenBoxRecord: "",
      total: "",
      loading: {
        list: false,
      }
    });

    const getMyOpenBoxRecord = async () => {
      state.loading.list = true;
      const res = await UserService.myOpenBoxRecord().finally(() => {
        state.loading.list = false;
      });
      state.myOpenBoxRecord = res.data.data;
      state.total = res.data.data.total;
    };

    const currentChange = async (val) => {
      const res = await UserService.myOpenBoxRecord({
        page: val,
      });
      state.myOpenBoxRecord = res.data.data;
    };

    onMounted(() => {
      getMyOpenBoxRecord();
    });

    return {
      ...toRefs(state),
      currentChange,
    };
  },
};
</script>
